{include file="common/header" /}

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input" >
                            </div>
                        </div>
                        
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary" lay-submit  lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm " onclick="addclick()" > 添加 </button>
                {if session("admin_id") == 1 /}
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn"> 删除 </button>
                {/if}
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            {{#  if(d.admin_id != 2){ }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            {{# } }}
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layuimini = layui.layuimini;

        table.render({
            elem: '#currentTableId',
            url: "{:url('admin/list')}",
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            /*defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],*/
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'admin_id', width: 80, title: 'ID', sort: true},
                {field: 'username',  title: '用户名'},
                {field: 'role_name', width: 150, title: '角色'},
                {field: 'add_time', width: 160, title: '添加时间', sort: true},
                {title: '操作', width: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            // //执行搜索重载
            // table.reload('currentTableId', {
            //     page: {
            //         curr: 1
            //     }
            //     , where: {
            //         searchParams: result
            //     }
            // }, 'data');

            table.reload('currentTableId',{
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
            return false;
        });

        // 监听添加操作
        /*$(".data-add-btn").on("click", function () {
            layerOpenWindow("{:url('admin/add')}","添加用户",['80%', '80%'],true);

            return false;
        });*/
        
        window.addclick = function(obj) {
            layerOpenWindow("{:url('admin/add')}","添加用户",['80%', '80%'],true);

            return false;
        }

        // 监听删除操作
        $(".data-delete-btn").on("click", function () {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
                console.log(data);

            var ids = '';
            if (data.length == 0) {
                layer.msg("请选择数据",{icon:2})
                return false
            } else {
                for (var i = 0; i < data.length; i++) {
                    ids += data[i].admin_id + ',';
                }
                ids=ids.substring(0,ids.length-1);
            }
            console.log(ids)

            $.ajax({
                url:"{:url('admin/deleteMore')}",
                data: {ids:ids},
                type:'post',
                dataType:'json',
                success:function(res){
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });



            // layer.alert(JSON.stringify(data));
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                layerOpenWindow("{:url('admin/edit')}?admin_id="+data.admin_id,"修改用户",['80%', '80%'],true);

                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除吗', function (index) {
                    
                    $.ajax({
                        url:"{:url('admin/delete')}",
                        data: {id:data.admin_id},
                        type:'post',
                        dataType:'json',
                        success:function(res){
                            if(res.errcode == 1){
                                layer.msg(res.message,{icon: 1,time: 1000},function(){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    location.reload();
                                })
                            }else {
                                layer.msg(res.message,{icon: 2,time: 2000})
                            }
                            // parent.layer.close(index);
                        },
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                            layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                        }
                    });

                });
            }
        });

    });
</script>

</body>
</html>